package com.ymatou.iapi.sellerproduct.testcase;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.parameter.ProdTplBean;
import com.ymatou.iapi.sellerproduct.service.AddLiveProductCall;
import com.ymatou.iapi.sellerproduct.service.AddNewSellerProductCall;
import com.ymatou.iapi.sellerproduct.service.SaveProductTemplateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtProductsInLive;
import com.ymttest.database.mongo.LiveBasicInfoBean;
import com.ymttest.database.mongo.LiveBasicInfoWapper;
import com.ymttest.database.mongo.LiveProductInfoWapper;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtProductDescMapWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_AddLiveProduct {
	private static AddNewSellerProductCall addnewsellerproductCall = new AddNewSellerProductCall();
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	//private static String activityNotStartId = EnvSetup.getData("productActivityNotStartId");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("App-添加直播商品");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new AddLiveProductCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@SuppressWarnings("unchecked")
	public void Tc_AddLiveProduct_001() {
		Logger.start(true, "添加直播商品(正在进行中的直播)");
		try{
			Map<String, Object> tgtRetMap = new HashMap<String,Object>();
			//1 add new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, Integer.valueOf(sellerId));
			Logger.verifyEquals(liveResponseBean.getInt("Code"), 200, "响应报文-返回码");
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add live product
			//JSONObject productResponseBean = new AddLiveProductCall().addLiveProductWithLiveId(sellerId, liveId);
			JSONObject productResponseBean = new AddLiveProductCall().callAndGetReturnData(MapUtil.hashMap("LoginId", sellerName, "UserId", sellerId, "Product->ActivityId", liveId , "Product->IsNewProduct", true));
			Logger.verifyEquals(productResponseBean.getInt("Code"), 200, "响应报文-返回码");
			String productId = productResponseBean.getJSONObject("Data").getString("ProductId");
			
			
			JSONObject addProductBean = productResponseBean.getJSONObject("JsonBeanRequest");
			
			//check SQL
			YmtProductsInLive ymtProductsInLive = new YmtProductsInLiveWapper().selectByLiveId(liveId).get(0);
			Map<String, Object> sqlTgtMap = MapUtil.hashMap("iuserid", sellerId, "sproductid", productId, "iactivityid", liveId ,"iAction", 0);
			
			AssertService.assertResultEqual(sqlTgtMap, MapUtil.pojoToMap(ymtProductsInLive));
			
			
			//Check Mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//ProductBasicInfo
				Map<String, Object> ProductBasicInfoMap = new ProductBasicInfoWapper().getByProductId(productId);
				Logger.verifyEquals(true,Boolean.parseBoolean(ProductBasicInfoMap.get("IsLiveProduct").toString()), "Mongo-ProductBasicInfo IsLiveProduct");
				
				// LiveBasicInfo
				LiveBasicInfoBean liveBasicInfoBean = new LiveBasicInfoWapper().getByActivityId(liveId);
				Logger.verifyEquals(liveId, liveBasicInfoBean.getActivityId(), "Mongo-LiveBasicInfo LiveId");
				
				//LiveProductInfo 
				Map<String, Object> liveProductInfoMap = new LiveProductInfoWapper().getliveproductByProductId(productId);
				tgtRetMap.clear();
				tgtRetMap.put("LiveId", liveId);
				tgtRetMap.put("ProductId", productId);
				tgtRetMap.put("SellerId", sellerId);
				AssertService.assertResultEqual(tgtRetMap, liveProductInfoMap);
			}
			

			//新Mongo断言
			if (YmtMongoDriverFactroy.isNewMongoCheck()){	
				
				//LiveProductModel
				addnewsellerproductCall.checkNewMongoLiveProductModel(productResponseBean, productId, sellerId);
				
				//LiveModel
				LiveCallService.checkNewMongoLiveModel(liveResponseBean,productResponseBean,liveId);
				
			}
			
			//首次露出时间、结束时间断言
			SellerProductCallService.checkListingTime(productId, null);

			
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){	
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@SuppressWarnings("unchecked")
	public void Tc_AddLiveProduct_002() {
		Logger.start(true, "添加直播商品(即将开始的直播)");
		try{
			Map<String, Object> tgtRetMap = new HashMap<String,Object>();
			//1 add new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveBeginInFuture(sellerName, Integer.valueOf(sellerId));
			Logger.verifyEquals(liveResponseBean.getInt("Code"), 200, "响应报文-返回码");
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add live product
			//JSONObject productResponseBean = new AddLiveProductCall().addLiveProductWithLiveId(sellerId, liveId);
			JSONObject productResponseBean = new AddLiveProductCall().callAndGetReturnData(MapUtil.hashMap("LoginId", sellerName, "UserId", sellerId, "Product->ActivityId", liveId, "Product->IsNewProduct", true));
			Logger.verifyEquals(productResponseBean.getInt("Code"), 200, "响应报文-返回码");
			String productId = productResponseBean.getJSONObject("Data").getString("ProductId");
			
			
			JSONObject addProductBean = productResponseBean.getJSONObject("JsonBeanRequest");

			//check SQL
			YmtProductsInLive ymtProductsInLive = new YmtProductsInLiveWapper().selectByLiveId(liveId).get(0);
			Map<String, Object> sqlTgtMap = MapUtil.hashMap("iuserid", sellerId, "sproductid", productId, "iactivityid", liveId ,"iAction", 0);
			
			AssertService.assertResultEqual(sqlTgtMap, MapUtil.pojoToMap(ymtProductsInLive));
			
			//Check Mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//ProductBasicInfo
				Map<String, Object> ProductBasicInfoMap = new ProductBasicInfoWapper().getByProductId(productId);
				Logger.verifyEquals(true,Boolean.parseBoolean(ProductBasicInfoMap.get("IsLiveProduct").toString()), "Mongo-ProductBasicInfo IsLiveProduct");
				
				// LiveBasicInfo
				LiveBasicInfoBean liveBasicInfoBean = new LiveBasicInfoWapper().getByActivityId(liveId);
				Logger.verifyEquals(liveId, liveBasicInfoBean.getActivityId(), "Mongo-LiveBasicInfo LiveId");
				
				//LiveProductInfo 
				Map<String, Object> liveProductInfoMap = new LiveProductInfoWapper().getliveproductByProductId(productId);
				tgtRetMap.clear();
				tgtRetMap.put("LiveId", liveId);
				tgtRetMap.put("ProductId", productId);
				tgtRetMap.put("SellerId", sellerId);
				AssertService.assertResultEqual(tgtRetMap, liveProductInfoMap);
			}
			

			//新Mongo断言
			if (YmtMongoDriverFactroy.isNewMongoCheck()){	
				
				//LiveProductModel
				addnewsellerproductCall.checkNewMongoLiveProductModel(productResponseBean, productId, sellerId);
				
				//LiveModel
				LiveCallService.checkNewMongoLiveModel(liveResponseBean,productResponseBean,liveId);
				
			}
			
			//首次露出时间、结束时间断言
			SellerProductCallService.checkListingTime(productId, null);

			if (YmtMongoDriverFactroy.isNewMongoCheck()){	
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
		}catch(Exception e){
			Logger.fail(e);
		}
	}	
	
	
	
	
	
	
	//app 不支持属性创建和修改
	@Test
	@Category({P1.class})
	@TestCase
	@SuppressWarnings("unchecked")
	public void Tc_AddLiveProduct_003() {
		Logger.start(true, "添加直播商品 template ");
		try{
			//1 add new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveBeginInFuture(sellerName, Integer.valueOf(sellerId));
			Logger.verifyEquals(liveResponseBean.getInt("Code"), 200, "响应报文-返回码");
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add live product

			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			saveProductTemplateCall.deleteTemplate(sellerId);
			//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
			ProdTplBean sizeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 1);
			ProdTplBean buyerNoticeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 2);
			ProdTplBean sellerServiceTemplate = saveProductTemplateCall.saveTemplate(sellerId, 3);
		

			Map<String, Object> updateMap = MapUtil.hashMap();
			updateMap.put("LoginId", sellerName);
			updateMap.put("UserId", sellerId);
			updateMap.put("Product->ActivityId", liveId);
			updateMap.put("Product->IsNewProduct", true);
			updateMap.put("Product->SizeTemplateId", sizeTemplate.getTemplateId());
			updateMap.put("Product->BuyerNoticeId", buyerNoticeTemplate.getTemplateId());
			updateMap.put("Product->SellerServiceId", sellerServiceTemplate.getTemplateId());
			
			JSONObject productResponseBean = new AddLiveProductCall().callAndGetReturnData(updateMap);
			Logger.verifyEquals(productResponseBean.getInt("Code"), 200, "响应报文-返回码");
			String productId = productResponseBean.getJSONObject("Data").getString("ProductId");
			
			//模板检查
			//check product template
			SellerProductCallService.checkTemplateWithProd(productId, Arrays.asList(sizeTemplate,buyerNoticeTemplate,sellerServiceTemplate));
			
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	
//	
//	/**
//	 * 新增商品断言
//	 * @param jsonObjectBean 发布商品请求报文
//	 * @param productID 商品id
//	 * @return 
//	 * @throws Exception
//	 */
//	private static void checkOldProductBasicInfoForLiveProduct (JSONObject jsonObjectBean,String productID) throws Exception {
//	    try {
//	    	//取出请求报文的参数
//	    	JSONObject productObject=  jsonObjectBean.getJSONObject("Product");
//	    	JSONObject productDescriptionInfoObject = productObject.getJSONObject("ProductDescriptionInfo");
//	    	JSONObject catalogs0 = productObject.getJSONArray("Catalogs").getJSONObject(0);
//	    	
//	    	int iAcceptReturn = productObject.getInt("AcceptReturn");
//	    	int iBrandId=(int) productObject.getInt("iBrandId");	//商品品牌
//	    	int userid=(int) jsonObjectBean.getInt("UserId");
//	    	String loginId=(String) jsonObjectBean.getString("LoginId");
//	    	String productname=(String) productObject.getString("ProductName");	//商品名
//	    	String mobileDescription=(String) productObject.getString("MobileDescription");	//移动端商品描述
//	    	String wordContent=(String) productDescriptionInfoObject.getString("WordContent"); 	//商品描述	    	  	
//	    	int limitnum=(int) productObject.getInt("LimitNum");		//限购数量
//	    	int iCatalogType=(int) productObject.getInt("CatalogType");	//2FBX商品
//	    	int iCatalogStatus=(int) productObject.getInt("CatalogStatus");	//3FBX海外仓 5FBX保税仓
//	    	String limitNumStartTime=String.format("{0:d}", (String) productObject.getString("LimitNumStartTime"));	//限购开始时间	    	
//	    	boolean noticeRisk=(boolean) productObject.getBoolean("NoticeRisk");	//砍单风险		    	
//	    	int tariffType=(int) productObject.getInt("TariffType");	//包税	    	
//	    	int onSaleType=(int) productObject.getInt("OnSaleType");	//上架状态
//	    	int localReturn=(int) productObject.getInt("LocalReturn");		//1官方退货 2买手本土退货
//	    	boolean noReasonReturn=(boolean) productObject.getBoolean("NoReasonReturn");	//无理由退货
//	    	int iThirdCategoryId=(int) productObject.getInt("iThirdCategoryId");	//三级类目	    
//	    	String description=(String) productObject.getString("Description");	//老商品详情描述
//
//	    	
//	    	//商品类目
//	    	YmtProductCategoryWapper ymtProductCategoryWapper = new YmtProductCategoryWapper();
//	    	Map<String, Object> ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(iThirdCategoryId).get(0);
//	    	String ThirdCategoryName = ret.get("sCategory").toString();
//	    	int iCategoryId = Integer.parseInt(ret.get("iMasterCategory").toString());
//	    	
//	    	ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(iCategoryId).get(0);
//	    	String CategoryName = ret.get("sCategory").toString();
//	    	int MasterCategoryId = Integer.parseInt(ret.get("iMasterCategory").toString());
//	    	
//	    	ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(MasterCategoryId).get(0);
//	    	String MasterCategoryName = ret.get("sCategory").toString();
//	    	
//	    	//品牌
//			List<Map> productBrand = new YmtProductBrandWapper().getProductBrandByBrandId(iBrandId);
//
//	    	//组装期待值对比
//	    	Map<String , Object> expectedMap = new HashMap<String , Object>();
//	    	expectedMap.put("AcceptReturn",iAcceptReturn);
//	    	expectedMap.put("Action",0);
//	    	expectedMap.put("BrandId",iBrandId);
//			if (iBrandId != 0) {
//				expectedMap.put("BrandName",(String) productBrand.get(0).get("sBrand"));		//品牌英文名
//				expectedMap.put("BrandEnName",(String) productBrand.get(0).get("sBrandEn"));		//品牌中文名
//			}else{
//				expectedMap.put("BrandName", jsonObjectBean.getJSONObject("JsonBeanRequest").getJSONObject("Product").getString("sBrandName"));		//自定义品牌名
//			}
//	    	
//	    	expectedMap.put(key, "BrandName");
//	    	expectedMap.put(key, "BrandEnName");
//	    	expectedMap.put(key, "CatalogType");
//	    	expectedMap.put(key, "MasterCategoryId");
//	    	expectedMap.put(key, "MasterCategoryName");
//	    	expectedMap.put(key, "CategoryId");
//	    	expectedMap.put(key, "CategoryName");
//	    	expectedMap.put(key, "ThirdCategoryId");
//	    	expectedMap.put(key, "ThirdCategoryName");
//	    	expectedMap.put(key, "DeliveryMethod ");
//	    	expectedMap.put(key, "DeliveryTemplateId ");
//	    	expectedMap.put(key, "Freight");
//	    	expectedMap.put(key, "IsLiveProduct");
//	    	expectedMap.put(key, "IsNewDesc");
//	    	expectedMap.put(key, "LastRefreshTime");
//	    	expectedMap.put(key, "LimitNum");
//	    	expectedMap.put(key, "LocalReturn");
//	    	expectedMap.put(key, "NoReasonReturn");
//	    	expectedMap.put(key, "MaxCatalogPrice");
//	    	expectedMap.put(key, "MinCatalogPrice");
//	    	expectedMap.put(key, "Name");
//	    	expectedMap.put(key, "NoticeRisk");
//	    	expectedMap.put(key, "OriginalProductId");
//	    	expectedMap.put(key, "ProdId");
//	    	expectedMap.put(key, "ProductId");
//	    	expectedMap.put(key, "TariffType");
//	    	expectedMap.put(key, "ValidEnd");
//	    	expectedMap.put(key, "ValidStart");
//	    	expectedMap.put(key, "Weight");
//	    	expectedMap.put(key, "MobileDescription");
//	    	expectedMap.put(key, "AutoLongSale");
//	    	expectedMap.put(key, "MainPicUrl");
//	    	expectedMap.put(key, "PicList");
//	    	expectedMap.put(key, "BondedArea");
//	    	expectedMap.put(key, "LivePicList");
//
//	    	
//        	//老Mongo ProductBasicInfo
//        	Map<String, Object>  productBasicInfo = new ProductBasicInfoWapper().getByProductId(productID);
//	        
//        	
//
//			
//	    }catch (Exception e) {
//	        e.printStackTrace();
//	    }
//	}
//	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}


















